﻿Public Class Main
    Dim xLevel As Integer = 1
    Dim xCurrentLevel As Integer = 1
    Dim xHighestScore() As String
    Dim xCoorChicken = {{27, 395}, {133, 440}, {252, 419}, {27, 498}, {221, 505}}
    Dim xCoorCow = {{275, 253}, {445, 264}, {614, 288}}
    Public CoorChicken(5) As Boolean
    Public CoorCow(3) As Boolean
    Dim i As Integer
    Public Sub DrawTransparentBG()
        'This listing for draw white semi transparent bg for label
        BgScore.BackColor = Color.FromArgb(200, Color.White)
        LabelScore.BackColor = Color.FromArgb(200, Color.White)
        LabelScorePrefix.BackColor = Color.FromArgb(200, Color.White)
        LabelHighest.BackColor = Color.FromArgb(200, Color.White)
        BgStatistic.BackColor = Color.FromArgb(200, Color.White)
        LabelStatistic.BackColor = Color.FromArgb(200, Color.White)
        LabelCow.BackColor = Color.FromArgb(200, Color.White)
        LabelChicken.BackColor = Color.FromArgb(200, Color.White)
        LabelKilled.BackColor = Color.FromArgb(200, Color.White)
        KilledTotal.BackColor = Color.FromArgb(200, Color.White)
        CowTotal.BackColor = Color.FromArgb(200, Color.White)
        ChickenTotal.BackColor = Color.FromArgb(200, Color.White)
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Read highest score from text
        xHighestScore = Split(System.IO.File.ReadAllText("HighestScore.txt"))
        LabelHighest.Text = "Highest Score : " & xHighestScore(0)

        TimerGlobal.Enabled = True
        DrawTransparentBG()
        FarmAnimalTimeStart()

        'preserve coordinate
        CoorChicken(0) = True
        CoorChicken(1) = True
        CoorCow(1) = True
    End Sub

    Private Sub BtnMainQuit_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMainQuit.Click
        'Quiting game and back to GameForm

        FarmAnimalTimeStop()
        TimerGlobal.Stop()

        Dim Obj As New Point
        Obj.PassedText = LabelScore.Text
        Obj.HighScore = LabelHighest.Text
        Obj.xchickentotal = ChickenTotal.Text
        Obj.xcowtotal = CowTotal.Text
        Obj.Killeds = KilledTotal.Text
        Obj.Show()
    End Sub

    Private Sub BtnMainHammer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMainHammer.Click
        'Listing code for give damage to tiger
        For Each ObjTiger In Me.Controls
            If TypeOf ObjTiger Is Tiger Then
                ObjTiger.HealthMeter.Height -= 5
                ObjTiger.HealthMeter.Top += 5

                'Check tiger live or die
                If ObjTiger.HealthMeter.Height <= 0 Then
                    KilledTotal.Text += 1
                    ObjTiger.Dispose()

                    Dim prize As Integer
                    prize = KilledTotal.Text * xLevel * 1000
                    'Give the prize + 1000 point
                    LabelScore.Text += prize
                    MsgBox("You kill the tiger, prized " & prize)
                    'Upgrade Level
                    xLevel += 1

                    FarmAnimalTimeStart()
                    TimerGlobal.Start()
                End If
            End If
        Next
    End Sub

    Public Sub FarmAnimalTimeStart()
        'Initiation for generate / start hunger foreach farm animal
        Dim ObjChicken As Object
        For Each ObjChicken In Me.Controls
            If TypeOf ObjChicken Is Chicken Then
                ObjChicken.TimerChickenHunger.Enabled = True
                ObjChicken.TimerBiten.Enabled = False
            End If
        Next
        Dim ObjCow As Object
        For Each ObjCow In Me.Controls
            If TypeOf ObjCow Is Cow Then
                ObjCow.TimerCowHunger.Enabled = True
                ObjCow.TimerBiten.Enabled = False
            End If
        Next
    End Sub

    Public Sub FarmAnimalTimeStop()
        'To kill all the timer process after the game over
        Dim ObjChicken As Object
        For Each ObjChicken In Me.Controls
            If TypeOf ObjChicken Is Chicken Then
                ObjChicken.TimerChickenHunger.Enabled = False
                ObjChicken.TimerBiten.Enabled = False
                ObjChicken.Timerlay.Enabled = False
            End If
        Next
        Dim ObjCow As Object
        For Each ObjCow In Me.Controls
            If TypeOf ObjCow Is Cow Then
                ObjCow.TimerCowHunger.Enabled = False
                ObjCow.TimerBiten.Enabled = False
                ObjCow.timerproduce.Enabled = False
            End If
        Next
    End Sub

    Private Sub TimerGlobal_Tick(sender As Object, e As EventArgs) Handles TimerGlobal.Tick
        'Timer tick for spawn the tiger

        'Spaw the tiger
        Dim xTiger As New Tiger
        xTiger.Left = 503
        xTiger.Top = 406
        xTiger.Level = xLevel + 1
        xLevel = xTiger.Level
        Me.Controls.Add(xTiger)
        TimerGlobal.Stop()
        'Upgrade the tiger level to make faster damage in next spawn

        Dim ObjCow As Object
        For Each ObjCow In Me.Controls
            If TypeOf ObjCow Is Cow Then
                'Activate the biten timer & make the interval faster by the level of tiger
                ObjCow.TimerBiten.Interval = 5000 / xLevel
                ObjCow.TimerBiten.Enabled = True
                ObjCow.TimerCowHunger.Stop()
            End If
        Next

        Dim ObjChicken As Object
        For Each ObjChicken In Me.Controls
            If TypeOf ObjChicken Is Chicken Then
                'Activate the biten timer & make the interval faster by the level of tiger
                ObjChicken.TimerBiten.Interval = 5000 / xLevel
                ObjChicken.TimerBiten.Enabled = True
                ObjChicken.TimerChickenHunger.Stop()
            End If
        Next
    End Sub

    Private Sub BtnMainChicken_Click(sender As Object, e As EventArgs) Handles BtnMainChicken.Click
        'Buy Chicken
        For i As Integer = 0 To 4
            CoorChicken(i) = False
        Next

        'Check Reserved coordinate
        Dim ObjChicken As Object
        For Each ObjChicken In Me.Controls
            If TypeOf ObjChicken Is Chicken Then
                CoorChicken(ObjChicken.Pos) = True
            End If
        Next

        'Check Capacity
        Dim ChickenCapacity As Integer
        For i As Integer = 0 To 4
            If CoorChicken(i) = True Then
                ChickenCapacity += 1
            Else
            End If
        Next

        If ChickenCapacity >= 5 Then
            MsgBox("Full")
        ElseIf LabelScore.Text >= 1000 Then

            'Draw Chicken on the form
            For i As Integer = 0 To 4
                If CoorChicken(i) = False Then
                    Dim xChicken As New Chicken
                    xChicken.Left = xCoorChicken(i, 0)
                    xChicken.Top = xCoorChicken(i, 1)
                    CoorChicken(i) = True
                    xChicken.Pos = i
                    xChicken.ChickenOld = 1
                    xChicken.ChickenImage.Image = My.Resources.littlechicken

                    'Check tiger existence
                    For Each ObjTiger In Me.Controls
                        If TypeOf ObjTiger Is Tiger Then
                            xChicken.TimerBiten.Interval = 5000 / xLevel
                            xChicken.TimerBiten.Enabled = True
                            xChicken.TimerChickenHunger.Stop()
                        Else
                            xChicken.TimerChickenHunger.Enabled = True
                        End If
                    Next
                    Me.Controls.Add(xChicken)

                    i = 5
                End If
            Next

            'Add Chicken total
            ChickenTotal.Text += 1
            'Minus point for 1000
            LabelScore.Text -= 1000
        Else
            MsgBox("Not enough point to buy chicken")
        End If
    End Sub

    Private Sub BtnMainCow_Click(sender As Object, e As EventArgs) Handles BtnMainCow.Click
        'Buy Cow
        For i As Integer = 0 To 2
            CoorCow(i) = False
        Next

        'Check Reserved coordinate
        Dim ObjCow As Object
        For Each ObjCow In Me.Controls
            If TypeOf ObjCow Is Cow Then
                CoorCow(ObjCow.Pos) = True
            End If
        Next

        'Check Capacity
        Dim CowCapacity As Integer
        For i As Integer = 0 To 2
            If CoorCow(i) = True Then
                CowCapacity += 1
            Else
            End If
        Next

        If CowCapacity >= 3 Then
            MsgBox("Full")
        ElseIf LabelScore.Text >= 2000 Then

            'Draw Chicken on the form
            For i As Integer = 0 To 2
                If CoorCow(i) = False Then
                    Dim xCow As New Cow
                    xCow.Left = xCoorCow(i, 0)
                    xCow.Top = xCoorCow(i, 1)
                    CoorCow(i) = True
                    xCow.Pos = i
                    xCow.CowOld = 1
                    xCow.CowImage.Image = My.Resources.smallcow

                    'Check tiger existence
                    For Each ObjTiger In Me.Controls
                        If TypeOf ObjTiger Is Tiger Then
                            xCow.TimerBiten.Interval = 5000 / xLevel
                            xCow.TimerBiten.Enabled = True
                            xCow.TimerCowHunger.Stop()
                        Else
                            xCow.TimerCowHunger.Enabled = True
                        End If
                    Next
                    Me.Controls.Add(xCow)

                    i = 3
                End If
            Next

            'Add Chicken total
            CowTotal.Text += 1
            'Minus point for 1000
            LabelScore.Text -= 2000
        Else
            MsgBox("Not enough point to buy cow")
        End If
    End Sub
End Class